
//this is app
this.app = (function (window, document) {
    let common = {};
    return {
        init:()=> {
            console.log('init');
        },
        // 路由模块
        route:(config)=> {
            config = config || {};
            config.$request = null;
            config.$target = config.$target || '';
            this.href = (path) => {
                if(config.$request) config.$request.abort();
                config.$request = $.get(path, function(res){
                    $(config.$target).html(res);
                });
            }
            this.bind = ()=> {
                let $self = this;
                $('body').on("click", 'a', function(event) {
                    console.log(event); 
                    let $path = $(this).attr('route-href');
                    if($path) $self.href($path);
                });
            }
            return this;
        },
        // 刷新iframe
        iframeReload:()=> {
            $("iframe[name='rightFrame']")[0].contentWindow.location.reload(true);
        },
        // 文件上传
        uploadFile:()=> {
            // 添加文件 
            this.add_file = (element)=> {
                var container = $(element).attr('data-container') || '#Plist';	
                var input_name = $(element).attr('data-input_name') == undefined ? 'pic' : $(element).attr('data-input_name');
                var filename = $(element).attr('data-filename') == undefined ? 'name' : $(element).attr('data-filename');
                var _len = $(container+'>div').length+1;	

                if($(element).attr('data-max') >= _len) {
                    let $html = '<div class="zss-pic-items">\
                                    <div class="zss-picbox" id="s_'+input_name+'_'+_len+'"></div>\
                                    <input type="hidden" name="'+input_name+'[]" id="'+input_name+'_'+_len+'" value="">\
                                    <iframe src="/plugins/upload/'+filename+'?name='+input_name+'_'+_len+'" width="100%" height="45" scrolling="no" frameborder="0"></iframe>  \
                                    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" onclick="app.uploadFile().remove_file(this)"><i class="layui-icon">&#xe640;</i> 移除</button>\
                                </div>';
                    $(container).append($html);
                }
                else {
                    alert('最多只能添加 '+ $(element).attr('data-max') +' 个文件');
                }
            }
            // 删除文件
            this.remove_file = (element)=> {	
                if(window.confirm('你确定移除该文件吗？')) 	{
                    $(element).parent().remove();
                }
            }
            // 删除文件
            this.multiple_remove = (element)=> {
                var _parent = $(element).parent().parent();
                var _srcArray = [];
                if(window.confirm('你确定移除该文件吗？'))	{
                    $(element).parent().remove();		
                    $.each($(_parent).find("li"), function(index, value) {
                        var _src = $(value).find("img").attr("src");
                            _src = _src.replace("/static/", "");
                        _srcArray.push(_src);
                    });		
                    $(_parent).siblings("input").val(_srcArray.join(','));
                    if(!_srcArray.length) $(_parent).html('<span>还没有上传图片。</span>');
                }
            }
            // 删除所有文件
            this.multiple_remove_all = (element)=> {
                if(window.confirm('你确定移除所有文件吗？'))	{
                    $('#s_' + element).html('<span>还没有上传图片。</span>');
                    $('#' + element).val('');
                }
            }
            // 获取所有图片路径
            this.get_multiple = (element)=> {
                var _parent = $('#s_' + element);
                var _srcArray = [];
                
                $.each(_parent.find("li"), function(index, value) {
                    var _src = $(value).find("img").attr("src");
                        _src = _src.replace("/static/", "");
                    _srcArray.push(_src);
                });		
                _parent.siblings("input").val(_srcArray.join(','));
                if(!_srcArray.length) _parent.html('<span>还没有上传图片。</span>');
            }
            return this;
        },
        // 多选框全选/取消
        checkAll:()=> {
            let form = $('#formCheck')[0];
            for(var i=0; i<form.elements.length; i++){
                var e = form.elements[i];
                console.log(e);
                if (e.name!='checkall') {
                    e.checked = form.checkall.checked;
                }
            }
        },
        //显示/隐藏侧边拦
        navDisplay:(obj)=> {
            let $ele = $(obj).find('i');
            if($ele.hasClass('layui-icon-shrink-right')) {
                $('.layui-layout-admin .layui-side, .layui-layout-admin .layui-logo').css({left:-200});
                $('.layui-body, .layui-header .layui-layout-left').css({left:0});

                $ele.removeClass('layui-icon-shrink-right').addClass('layui-icon-spread-left');
            }
            else {
                $('.layui-layout-admin .layui-side, .layui-layout-admin .layui-logo').css({left:0});
                $('.layui-body, .layui-header .layui-layout-left').css({left:200});

                $ele.removeClass('layui-icon-spread-left').addClass('layui-icon-shrink-right');
            }
            
        }
    };
})(window, document);